package com.example.framework;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import com.opera.core.systems.OperaDriver;

public class SeleniumHelper {

	private static WebDriver selenium;
	private StringBuffer verificationErrors = new StringBuffer();

	public SeleniumHelper(ApplicationManager manager) {		
		String browser = manager.getProperties("browser");
		if ("firefox".equals(browser)) {
			selenium = new FirefoxDriver();
		} else if ("ie".equals(browser)){
			selenium = new InternetExplorerDriver();
		} else if ("chrome".equals(browser)){
			selenium = new ChromeDriver();
		} else if ("opera".equals(browser)){
			selenium = new OperaDriver();
		} else {
			selenium = new HtmlUnitDriver();
		}
		getSelenium().manage().timeouts().implicitlyWait(Integer.parseInt(manager.getProperties("implicitWait", "10")), TimeUnit.SECONDS);
		
		String urls = manager.getProperties("baseUrl");
		selenium.get(urls);
	}
	
	public void finish() {
		getSelenium().quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	public WebDriver getSelenium() {
		return selenium;
	}

}
